<?php

class Game
{
  /** @var integer */
  public $id;
  /** @var integer */
  public $categoryId;
  /** @var string */
  public $name;
  /** @var string */
  public $title;
  /** @var integer */
  public $imageId;
  /** @var integer */
  public $views;
  /** @var float */
  public $rating;
  /** @var integer */
  public $ratingCount;
  /** @var integer */
  public $status;
  /** @var integer */
  public $width;
  /** @var integer */
  public $height;


  public static $STATUS_PRIVATE = 0;
  public static $STATUS_PUBLIC = 1;

  public static $COLUMN_MAPPING = array(
    'id' => 'id',
    'category_id' => 'categoryId',
    'name' => 'name',
    'title' => 'title',
    'image_id' => 'imageId',
    'views' => 'views',
    'rating' => 'rating',
    'rating_count' => 'ratingCount',
    'status' => 'status',
    'width' => 'width',
    'height' => 'height'
  );


  public function __construct($id = null, $name = null)
  {
    $this->Init();

    if (!$this->Load($id, $name))
      $this->Init();
  }

  private function Init()
  {
    $this->id = -1;
    $this->width = 680;
    $this->height = 360;
    $this->views = 0;
    $this->rating = 0;
    $this->ratingCount = 0;
    $this->status = 1;
  }
  
  private function Load($id = null, $name = null)
  {
    if (!$this->LoadData($id, $name)) return false;
    return true;
  }
  
  private function LoadData($id = null, $name = null)
  {
    $where = 1;

    if (is_numeric($id))
      $where .= " AND id='" . $id . "'";
    else if(is_string($name) && $name != "")
      $where .= " AND name='" . $name . "'";
    else
      return false; 

    $query = "SELECT g.* "
            ." FROM games AS g"
            ." WHERE $where";

    $rows = DB::query($query);

    if (count($rows) < 1) return false;

    $data = $rows[0];
    $this->SetPropertyValues((array)$data);

    return true;
  }


  private function SetPropertyValues($data)
  {
    $this->id          = $data['id'];
    $this->categoryId  = $data['category_id'];
    $this->name        = $data['name'];
    $this->title       = $data['title'];
    $this->imageId     = $data['image_id'];
    $this->views       = $data['views'];
    $this->rating      = $data['rating'];
    $this->ratingCount = $data['rating_count'];
    $this->status      = intval($data['status']);
    $this->width        = $data['width'];
    $this->height      = $data['height'];
  }
  
  public function GenerateSwfUrl()
  {
    $url = IMAGE_HOSTING . SWF_URI . $this->name . '.swf';
    return $url;
  }
  
  public function GenerateImageUrl($size = null)
  {
    if($size == 'small')
      $path = SMALL_IMAGES_URI;
    else if($size == 'big')
      $path = BIG_IMAGES_URI;
    $url = IMAGE_HOSTING . $path . $this->imageId . '.jpg';
    return $url;
  }
  
  public function GenerateGameUrl()
  {
    $category = new Category($this->categoryId);
    return 'games/' . $category->name . '/' . $this->name;
  }
  
  public function Save()
  {
    $values = array();
    
    $values['category_id'] = intval($this->categoryId);
    $values['name'] = $this->name;
    $values['title'] = $this->title;
    $values['image_id'] = $this->imageId;
    $values['views'] = $this->views;
    $values['rating'] = $this->rating;
    $values['rating_count'] = $this->ratingCount;
    $values['status'] = $this->status;
    $values['width'] = intval($this->width); // not null
    $values['height'] = $this->height;
 
    
    
    $oldGame = new Game($this->id);
    if ($oldGame->id && $oldGame->id > 0)
    {
      $res = DB::table('games')
              ->where('id', '=', $oldGame->id)
              ->update($values);
    }
    else
      $res = DB::table('games')->insert($values);

  }
  
  public function Delete()
  {

  }
  
  public static function GetLastGameId()
  {
    return DB::table('games')->max('id');
  }


  public static function &LoadGames($where = null, $join = null, $suffix = null, $columns = null)
  {
    $games = array();

    $query = "SELECT DISTINCT g.* $columns"
            ." FROM games AS g $join"
            ." WHERE 1 $where $suffix";

    $rows = DB::query($query);
    for ($i = 0, $count = count($rows); $i < $count; $i++)
    {
      $game = new Game();
      $game->SetPropertyValues((array) $rows[$i]);
      $games[] = $game;
    }
    
    return $games;
  }

  public static function BuildOrderByClause(&$paginationData)
  {
    $dir = strtolower($paginationData['dir']) == "asc" ? "asc" : "desc";
    $sort = $paginationData['sort'];
    $fields = Category::$COLUMN_MAPPING;
    $orderClause = "";
    if (array_key_exists($sort, $fields))
    {
      $orderClause = " ORDER BY c.{$fields[$sort]} {$dir}";
    }
    return $orderClause;
  }



  public static function CheckNameUniqueness($value, $categoryId = null)
  {
    $where = "name=" . ToSqlQuotedString($value);
    if (ValidId($categoryId))
      $where .= " AND id<>" . ToSqlQuotedString($categoryId);
    return !RowExists('categories', $where);

  }
    
}
?>
